﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;
using System.ComponentModel;

namespace ICeFramework.Win.UI
{
    public class PropertyComparer : FlowLayoutPanel
    {
        private Type _type;

        private object _leftObject;
        #region public object LeftObject
        /// <summary>
        /// Get/Sets the LeftObject of the PropertyComparer
        /// </summary>
        /// <value></value>
        public object LeftObject
        {
            get { return _leftObject; }
            set
            {
                _leftObject = value;
                if (_leftObject != null)
                {
                    _type = _leftObject.GetType();
                }
            }
        }
        #endregion

        private object _rightObject;
        #region public object RightObject
        /// <summary>
        /// Get/Sets the RightObject of the PropertyComparer
        /// </summary>
        /// <value></value>
        public object RightObject
        {
            get { return _rightObject; }
            set { _rightObject = value; }
        }
        #endregion

        private Dictionary<Type, string> _excludedProperties = new Dictionary<Type, string>();
        #region public Dictionary<Type, string> ExcludedProperties
        /// <summary>
        /// Get/Sets the ExcludedProperties of the PropertyComparer
        /// </summary>
        /// <value></value>
        public Dictionary<Type, string> ExcludedProperties
        {
            get { return _excludedProperties; }
            set { _excludedProperties = value; }
        }
        #endregion

        Dictionary<int, object> _references = new Dictionary<int, object>();
        #region public Dictionary<int, object> References
        /// <summary>
        /// Get/Sets the References of the PropertyComparer
        /// </summary>
        /// <value></value>
        public Dictionary<int, object> References
        {
            get { return _references; }
            set { _references = value; }
        }
        #endregion

        private int _indent = 20;
        #region public int Indent
        /// <summary>
        /// Get/Sets the Indent of the PropertyComparer
        /// </summary>
        /// <value></value>
        public int Indent
        {
            get { return _indent; }
            set
            {
                if (value < 15)
                {
                    _indent = 15;
                }
                else
                {
                    _indent = value;
                }
            }
        }
        #endregion

        //private int _rowWidth;
        //#region public int RowWidth
        ///// <summary>
        ///// Get/Sets the RowWidth of the PropertyComparer
        ///// </summary>
        ///// <value></value>
        //public int RowWidth
        //{
        //    get { return _rowWidth; }
        //    set
        //    {
        //        _rowWidth = value;
        //        //this.Width = value;
        //    }
        //}
        //#endregion

        private int _rowHeight = 20;
        #region public int RowHeight
        /// <summary>
        /// Get/Sets the RowHeight of the PropertyComparer
        /// </summary>
        /// <value></value>
        public int RowHeight
        {
            get { return _rowHeight; }
            set { _rowHeight = value; }
        }
        #endregion

        private int _maxLevel = 0;
        #region public int MaxLevel
        /// <summary>
        /// Gets/Sets the MaxLevel of the PropertyComparer
        /// </summary>
        /// <value></value>
        [Browsable(false)]
        public int MaxLevel
        {
            get { return _maxLevel; }
            set { _maxLevel = value; }
        }
        #endregion

        private int _minPropertyWidth = 280;
        #region public int MinPropertyWidth
        /// <summary>
        /// Get/Sets the MinPropertyWidth of the PropertyComparer
        /// </summary>
        /// <value></value>
        public int MinPropertyWidth
        {
            get { return _minPropertyWidth; }
            set { _minPropertyWidth = value; }
        }
        #endregion

        #region public PropertyComparer()
        /// <summary>
        /// Initializes a new instance of the <b>PropertyComparer</b> class.
        /// </summary>
        public PropertyComparer()
        {
            InitializeComponent();
            //_rowWidth = 1000;
        }
        #endregion

        #region public void GetProperties()
        /// <summary>
        /// 
        /// </summary>
        public void GetProperties()
        {
            this.ResizeRedraw = true;
            this.SuspendLayout();
            //_rowWidth = this.Width;
            this.Controls.Clear();
            string path = string.Empty;
            _references.Clear();
            PropertyComparerRow row = new PropertyComparerRow(this, null, false, path, string.Empty, _leftObject, _rightObject);
            _maxLevel = row.LevelCount;
            //ResizeToFit(row);

            this.Controls.Add(row);
            this.ResumeLayout();
        }
        #endregion

        private void ResizeToFit(PropertyComparerRow row)
        {
            int propertyWidth = row.Width - row.LevelCount * this.Indent * 2 - row.btnExpandLeft.Left - row.btnExpandLeft.Width;
            if (propertyWidth < _minPropertyWidth)
            {
                //_rowWidth = _minPropertyWidth * 2 + row.LevelCount * this.Indent * 2;
                //row.Width = _rowWidth;
                row.btnExpandRight.Left = row.Width - row.btnExpandRight.Width - 3;
            }
            else
            {
                row.Width = this.Width;
                row.btnExpandRight.Left = row.Width - row.btnExpandRight.Width - 3;
            }
        }

        #region private void InitializeComponent()
        /// <summary>
        /// 
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // PropertyComparer
            // 
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            this.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.Margin = new System.Windows.Forms.Padding(0);
            this.Size = new System.Drawing.Size(1000, 100);
            this.ResumeLayout(false);

        }
        #endregion

        //private void PropertyComparer_SizeChanged(object sender, EventArgs e)
        //{
        //    if (this.Controls.Count > 0)
        //    {
        //        ResizeToFit(this.Controls[0] as PropertyComparerRow);
        //    }
        //}
    }
}
